/*
 * Copyright (c) 2021. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.kason.heat;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

import java.nio.charset.Charset;

/**
 * @author 卢清波
 * @date 2021/1/25 13:23
 */
public class HeatClient
{
    public static void main(String[] args) throws InterruptedException
    {
        conn();
    }

    public static void conn()
    {
        EventLoopGroup eventLoopGroup = new NioEventLoopGroup(10);
        try
        {
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(eventLoopGroup)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>()
                {
                    protected void initChannel(SocketChannel socketChannel) throws Exception
                    {
                        socketChannel.pipeline().addLast(new StringEncoder(Charset.forName("utf-8")));
                        socketChannel.pipeline().addLast(new StringDecoder(Charset.forName("utf-8")));
                        socketChannel.pipeline().addLast(new HeatClientHandler());
                    }
                });
        System.out.println("开始链接服务端。。。");
        ChannelFuture channelFuture = bootstrap.connect("localhost", 9000).sync();
        channelFuture.addListener(new ChannelFutureListener()
        {
            public void operationComplete(ChannelFuture channelFuture) throws Exception
            {
                if (channelFuture.isSuccess())
                {
                    System.out.println("链接服务端9000成功");
                }else{
                    System.out.println("链接服务端9000失败");
                }

            }
        });
        channelFuture.channel().closeFuture().sync();

    }
        catch (Exception ex)
    {
        ex.printStackTrace();
    }finally
    {
        eventLoopGroup.shutdownGracefully();
    }
    }


}
